//
//  CarouselFigure.m
//  CarouselFigure
//
//  Created by 雨天记忆 on 15/6/14.
//  Copyright (c) 2015年 雨天记忆. All rights reserved.
//

#import "CarouselFigure.h"
#import "UIImageView+WebCache.h"
#import "Define.h"
#import "CarouselFigureModel.h"
@interface CarouselFigure ()<UIScrollViewDelegate>

@property (retain, nonatomic) UIScrollView *scrollView;
@property (retain, nonatomic) UIPageControl *pageControl;
@property (retain, nonatomic) UILabel *text;//显示文本(这个弃用)
@property (nonatomic, strong) UILabel *titleLabel; //显示文本
@property (retain, nonatomic) NSMutableArray *curArray;//暂存当前页面显示的图片
@property (retain, nonatomic) NSMutableArray *curTitleArray;// 暂存当前页面标题
@property (retain, nonatomic) NSTimer *timer;
@property (nonatomic, strong) CarouselFigureModel *model;// 用来标记点击的model

@end

@implementation CarouselFigure
{
    CGFloat width;
    CGFloat height;
    NSInteger curPage;
}

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        width = frame.size.width;
        height = frame.size.height;
        self.curArray = [NSMutableArray arrayWithCapacity:3];
        self.curTitleArray = [NSMutableArray arrayWithCapacity:3];// 我的补充
        [self initScrollView];
        //[self initTextView];
        [self initTitleLable];  // 我修改为我的titleLabel
        [self initPageControl];
    }
    return self;
}

//初始化ScrollView
- (void)initScrollView{
    self.scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
    self.scrollView.height = self.bounds.size.height - 25; // 这行代码是我添加的
    self.scrollView.contentSize = CGSizeMake(width * 3, height - 25);
    self.scrollView.contentOffset = CGPointMake(width, 0);
    self.scrollView.delegate = self;
    self.scrollView.pagingEnabled = YES;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.showsVerticalScrollIndicator = NO;
    [self createCarouselFigureView];
    [self addSubview:self.scrollView];
}

// 初始化titleLable
- (void)initTitleLable{
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, self.scrollView.height, width - 20, 25)];
    self.titleLabel.textColor = [UIColor whiteColor];
    self.titleLabel.font = [UIFont boldSystemFontOfSize:width / 25];
    self.backgroundColor = [UIColor colorWithRed:112/225.0 green:112/225.0 blue:112/225.0 alpha:1];
    [self addSubview:self.titleLabel];
}

//创建三个视图到轮播图上边
- (void)createCarouselFigureView{
    for (int i = 0; i < 3; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(width * i, 0, width, height)];
        imageView.tag = 200 + i;
        imageView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pushToDetailView:)];
        [imageView addGestureRecognizer:tap];
        [self.scrollView addSubview:imageView];
    }
}

//初始化text文本视图
- (void)initTextView{
    self.text = [[UILabel alloc] initWithFrame:CGRectMake(0, height - 30, width, 30)];
    self.text.textColor = [UIColor redColor];
    [self addSubview:self.text];
}

//初始化PageControl视图
- (void)initPageControl{
    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(width - 80, height - 30, 80, 30)];
    [self addSubview:self.pageControl];
}

//更新当前的视图对应的索引
- (NSInteger)updatePage:(NSInteger)page{
    NSInteger count = self.array.count;
    return (count + page) % count;
}

//更新当前对应的视图
- (void)updateCurrentViewWithCurPage:(NSInteger)page{
    NSInteger pre = [self updatePage:page - 1];
    curPage = [self updatePage:page];
    NSInteger last = [self updatePage:page + 1];
    [self.curArray removeAllObjects];

    
    [self.curArray addObject:self.array[pre]];
    [self.curArray addObject:self.array[curPage]];
    [self.curArray addObject:self.array[last]];
    
    
    
    NSArray *subArray = self.scrollView.subviews;
    for (int i = 0; i < 3; i++) {
        UIImageView *imageView = subArray[i];
        
        CarouselFigureModel *model = self.curArray[i];
        NSURL *url = [NSURL URLWithString:model.banner_image_url];
        [imageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeHoldImage3"]];
        model = self.curArray[1];
        self.model = model;// 标记当前显示的model
        self.titleLabel.text = model.banner_name;

    }
    self.scrollView.contentOffset = CGPointMake(width, 0);
    self.pageControl.currentPage = curPage;//更新pageControl点的位置
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat x = scrollView.contentOffset.x;
    // >= or <= 是为了滑动时候更加流畅，不会因为滑动过快而卡顿.
    if (x >= width * 2) {
        [self updateCurrentViewWithCurPage:curPage + 1];
    }else if (x <= 0){
        [self updateCurrentViewWithCurPage:curPage - 1];
    }
}

- (void)setArray:(NSArray *)array{
    _array = array;
    self.pageControl.numberOfPages = array.count;
    [self updateCurrentViewWithCurPage:0];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(autoPlay) userInfo:nil repeats:YES];
}



- (void)autoPlay{
    [self.scrollView setContentOffset:CGPointMake(width * 2, 0) animated:YES];
    //虽然不是手动滑动视图，但是系统还是调用了
    //- (void)scrollViewDidScroll:(UIScrollView *)scrollView；这个方法。
}

- (void)pushToDetailView:(UITapGestureRecognizer *)tap
{
    UIImageView *imageView = (UIImageView *)tap.view;
    if (imageView.tag == 201) {
        [self.delegate didSelectCarouselPushDetailVcWith:self.model];   
    }
    
}

@end
